from aip import AipOcr
import sqlite3
import time
import re
import threading
import os

os.system('title 水位记录-baidu')
""" 你的 APPID AK SK """
APP_ID = '16375016'
API_KEY = 'CQgoSnEq902TsagaYt0Ub9iN'
SECRET_KEY = 'llGmluwd49dUZPGv5VH7WKV5XmqjXYer'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

# db_path = 'swdb.db'
db_path = r'D:\lljdata\swdb.db'


def gettext(tmpurl):
    """ 调用通用文字识别, 图片参数为远程url图片 """
    return client.basicGeneralUrl(tmpurl)


def getnowtime():
    return str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


def getsw():
    with open("gc.txt") as gcfile:
        gc = gcfile.read()
    gc = gc.split("/")
    txt = ""
    j = 0
    for i in gc:
        tmp = ""
        for s in i:
            tmp += "%" + s
        tmp += "%"
        if j > 0:
            txt += ' or "_name" like "' + tmp + '"'
        else:
            txt += '"_name" like "' + tmp + '"'
        j += 1
    print(getnowtime() + "开始导入数据")
    now_time = str(round(time.time()))
    conn = sqlite3.connect(db_path)
    c = conn.cursor()
    # sqltxt = 'SELECT "_id","_name" FROM gcdb WHERE ano = "A11"'
    # sqltxt = 'SELECT "_id", "_name" FROM gcdb WHERE "_name" like "%刘%涧%"'
    sqltxt = 'SELECT "_id", "_name" FROM gcdb WHERE' + txt
    print(sqltxt)
    # exit(0)
    records = c.execute(sqltxt)
    c = conn.cursor()

    for row in records:
        try:
            sw_keyword = row[0]
            sw_name = row[1]
            print(sw_keyword)
            print(sw_name)
            if sw_keyword != "":
                url = f"http://www.jsswj.com.cn:88/jsswxxSSI/static/map1/0/3/{sw_keyword}.png?t=" + now_time
                sw_contents = gettext(url)
                print(sw_contents)
                # sw_name = sw_contents["words_result"][0]["words"]
                sw_sysw = float(
                    re.findall(r"\d+\.?\d*",
                               sw_contents["words_result"][1]["words"])[0])
                sw_xysw = float(
                    re.findall(r"\d+\.?\d*",
                               sw_contents["words_result"][2]["words"])[0])
                if sw_sysw > 100:
                    sw_sysw /= 100
                    sw_sysw = str(sw_sysw)
                if sw_xysw > 100:
                    sw_xysw /= 100
                    sw_xysw = str(sw_xysw)
                sw_time = str(
                    time.strftime('%Y-%m-%d %H:%M:%S',
                                  time.localtime(time.time())))
                # print(sw_xysw)
                # print(sw_sysw)
                # print(sw_name)
                insertsql = 'INSERT OR IGNORE INTO  swdb ("name","sysw","xysw","time","keyword") VALUES(?,?,?,?,?)'
                c.execute(insertsql,
                          [sw_name, sw_sysw, sw_xysw, sw_time, sw_keyword])
                time.sleep(1)
        except Exception as e:
            print(e)
            pass
    # finally:
    conn.commit()
    conn.close()
    print(getnowtime() + "导入结束")

    timer = threading.Timer(3600, getsw)
    timer.start()


if __name__ == "__main__":
    getsw()
